﻿using Engine;
using Game;
using GameEntitySystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TemplatesDatabase;

namespace SCIENEW
{
	public class ComponentInserter : ComponentInventoryBase
	{
		protected ComponentBlockEntity m_componentBlockEntity;
		public SubsystemTerrain SubsystemTerrain;
		public SubsystemBlockEntities subsystemBlockEntities;
		public SubsystemPickables subsystemPickables;
		public int innum;
		public int outnum;
		public bool DispenseItem = true;

		public bool Place()
		{
			Point3 coordinates = m_componentBlockEntity.Coordinates;
			int cellValue = SubsystemTerrain.Terrain.GetCellValue(coordinates.X, coordinates.Y, coordinates.Z);
			//int num = 0;
			int value = 0;
			int slotCount = GetSlotCount(0);
			if (slotCount > 0)
				value = GetSlotValue(0);
			int face = DevicesUtils.GetInnerData(cellValue);
			Vector3 vector = CellFace.FaceToVector3(face);
			var position = new Vector3(coordinates) + new Vector3(0.5f);
			Point3 coor2 = coordinates - new Point3((int)vector.X, (int)vector.Y, (int)vector.Z);
			var entity = subsystemBlockEntities.GetBlockEntity((int)coor2.X, (int)coor2.Y, (int)coor2.Z);
			if (entity == null)
				return false;
			ComponentInventoryBase component = entity.Entity.FindComponent<ComponentInventoryBase>();
			if (component != null)
			{
				IInventory inventory = entity.Entity.FindComponent<ComponentInventoryBase>();
				int i2 = innum;
				int o = outnum;
				if (i2 == 0)
				{
					for (int i = 0; i < inventory.SlotsCount; i++)
					{
						if (inventory.GetSlotCount(i) > 0)
						{
							int num = 1;
							
							var a = entity.Entity.FindComponent<ComponentExCraftingTable>();
							if (a != null && i == a.ResultSlotIndex)
							{
								num = a.m_matchedRecipe != null ? a.m_matchedRecipe.ResultCount : a.GetSlotCount(i);
							}
							if (value != 0)
							{
								if (inventory.GetSlotValue(i) == value)
								{
									Vector3 vector2 = position + 0.6f * vector;

									Point3 coor3 = coordinates + new Point3((int)vector.X, (int)vector.Y, (int)vector.Z);
									var entity2 = subsystemBlockEntities.GetBlockEntity((int)coor3.X, (int)coor3.Y, (int)coor3.Z);
									if (entity2 != null)
									{
										ComponentInventoryBase component2 = entity2.Entity.FindComponent<ComponentInventoryBase>();
										if (component2 != null)
										{


											IInventory inventory2 = entity2.Entity.FindComponent<ComponentInventoryBase>();
											if (o == 0)
											{

												if (AcquireItems(inventory2, value, num) < 1)
												{
													//ComponentInventoryBase.
													inventory.RemoveSlotItems(i, num);
													return true;
													//inventory.re;
												}
											}
											else
											{
												if (inventory2.SlotsCount >= o && inventory2.GetSlotCount(o - 1) == 0 || (inventory2.GetSlotValue(o - 1) == value && inventory2.GetSlotCount(o - 1) + num <= inventory.GetSlotCapacity(o - 1, value)))
												{
													inventory2.AddSlotItems(o - 1, value, num);
													inventory.RemoveSlotItems(i, num);
													return true;
												}
											}
										}
									}
									else
									{
										subsystemPickables.AddPickable(value, num, vector2, 1.8f * (vector + m_random.Vector3(0.2f)), null);
										inventory.RemoveSlotItems(i, num);
										return true;
									}

								}
							}
							else
							{//v == 262384 || v == 786672 || v == 1048816 || v == 1310960 || v == WaterBlock.Index || v==MagmaBlock.Index
								int v = inventory.GetSlotValue(i);
								Vector3 vector2 = position + 0.6f * vector;
								Point3 coor3 = coordinates + new Point3((int)vector.X, (int)vector.Y, (int)vector.Z);
									var entity2 = subsystemBlockEntities.GetBlockEntity((int)coor3.X, (int)coor3.Y, (int)coor3.Z);
									if (entity2 != null)
									{
										ComponentInventoryBase component2 = entity2.Entity.FindComponent<ComponentInventoryBase>();
										if (component2 != null)
										{
											IInventory inventory2 = entity2.Entity.FindComponent<ComponentInventoryBase>();
											if (o == 0)
											{

												if (AcquireItems(inventory2, v, num) < 1)
												{
													//ComponentInventoryBase.
													inventory.RemoveSlotItems(i, num);
													return true;
													//inventory.re;
												}
											}
											else
											{
												if (inventory2.SlotsCount >= o && inventory2.GetSlotCount(o - 1) == 0 || (inventory2.GetSlotValue(o - 1) == v && inventory2.GetSlotCount(o - 1) + num <= inventory.GetSlotCapacity(o - 1, v)))
												{
													inventory2.AddSlotItems(o - 1, v, num);
													inventory.RemoveSlotItems(i, num);
													return true;
												}
											}
										}
									}
									else
									{
										subsystemPickables.AddPickable(v, num, vector2, 1.8f * (vector + m_random.Vector3(0.2f)), null);
										inventory.RemoveSlotItems(i, num);
										return true;
									}

							}
						}
					}
				}
				else
				{
					if (inventory.GetSlotCount(i2 - 1) > 0)
					{
						int num = 1;
						value = inventory.GetSlotValue(i2 - 1);
						var a = entity.Entity.FindComponent<ComponentExCraftingTable>();
						if (a != null && i2 - 1 == a.ResultSlotIndex)
						{
							num = a.m_matchedRecipe != null ? a.m_matchedRecipe.ResultCount : a.GetSlotCount(i2 - 1);
						}
						if (value != 0)
						{
							if (inventory.GetSlotValue(i2 - 1) == value)
							{
								Vector3 vector2 = position + 0.6f * vector;

								Point3 coor3 = coordinates + new Point3((int)vector.X, (int)vector.Y, (int)vector.Z);
								var entity2 = subsystemBlockEntities.GetBlockEntity((int)coor3.X, (int)coor3.Y, (int)coor3.Z);
								if (entity2 != null)
								{
									ComponentInventoryBase component2 = entity2.Entity.FindComponent<ComponentInventoryBase>();
									if (component2 != null)
									{
										IInventory inventory2 = entity2.Entity.FindComponent<ComponentInventoryBase>();
										if (o == 0)
										{

											if (AcquireItems(inventory2, value, num) < 1)
											{
												//ComponentInventoryBase.
												inventory.RemoveSlotItems(i2 - 1, num);
												return true;
												//inventory.re;
											}
										}
										else
										{
											if (inventory2.SlotsCount >= o && inventory2.GetSlotCount(o - 1) == 0 || (inventory2.GetSlotValue(o - 1) == value && inventory2.GetSlotCount(o - 1) + num <= inventory.GetSlotCapacity(o - 1, value)))
											{
												inventory2.AddSlotItems(o - 1, value, num);
												inventory.RemoveSlotItems(i2 - 1, num);
												return true;
											}
										}
									}
								}
								else
								{
									subsystemPickables.AddPickable(value, num, vector2, 1.8f * (vector + m_random.Vector3(0.2f)), null);
									inventory.RemoveSlotItems(i2 - 1, num);
									return true;
								}

							}
						}
						else
						{//v == 262384 || v == 786672 || v == 1048816 || v == 1310960 || v == WaterBlock.Index || v==MagmaBlock.Index
							int v = inventory.GetSlotValue(i2 - 1);
							{
								Vector3 vector2 = position + 0.6f * vector;

								Point3 coor3 = coordinates + new Point3((int)vector.X, (int)vector.Y, (int)vector.Z);
								var entity2 = subsystemBlockEntities.GetBlockEntity((int)coor3.X, (int)coor3.Y, (int)coor3.Z);
								if (entity2 != null)
								{
									ComponentInventoryBase component2 = entity2.Entity.FindComponent<ComponentInventoryBase>();
									if (component2 != null)
									{
										IInventory inventory2 = entity2.Entity.FindComponent<ComponentInventoryBase>();
										if (o == 0)
										{

											if (AcquireItems(inventory2, v, num) < 1)
											{
												//ComponentInventoryBase.
												inventory.RemoveSlotItems(i2 - 1, num);
												return true;
												//inventory.re;
											}
										}
										else
										{
											if (inventory2.SlotsCount >= o && inventory2.GetSlotCount(o - 1) == 0 || (inventory2.GetSlotValue(o - 1) == v && inventory2.GetSlotCount(o - 1) + num <= inventory.GetSlotCapacity(o - 1, v)))
											{
												inventory2.AddSlotItems(o - 1, v, num);
												inventory.RemoveSlotItems(i2 - 1, num);
												return true;
											}
										}
									}
								}
								else
								{
									subsystemPickables.AddPickable(v, num, vector2, 1.8f * (vector + m_random.Vector3(0.2f)), null);
									inventory.RemoveSlotItems(i2 - 1, num);
									return true;
								}
							}


						}
					}
				}

			}
			return false;
		}


		public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
		{
			base.Load(valuesDictionary, idToEntityMap);
			m_componentBlockEntity = Entity.FindComponent<ComponentBlockEntity>(true);
			SubsystemTerrain = Project.FindSubsystem<SubsystemTerrain>(true);
			subsystemBlockEntities = Project.FindSubsystem<SubsystemBlockEntities>(true);
			subsystemPickables = Project.FindSubsystem<SubsystemPickables>(true);
			//SubsystemPlayers = Project.FindSubsystem<SubsystemPlayers>(true);
			innum = valuesDictionary.GetValue<int>("Innum", 0);
			outnum = valuesDictionary.GetValue<int>("Outnum", 0);
		}

		public override void Save(ValuesDictionary valuesDictionary, EntityToIdMap entityToIdMap)
		{
			base.Save(valuesDictionary,entityToIdMap);
			valuesDictionary.SetValue("Innum", innum);
			valuesDictionary.SetValue("Outnum", outnum);
		}
	}
}
